package com.todod.config;

import java.util.concurrent.TimeUnit;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.CacheControl;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import com.todod.interceptor.LoginInterceptor;

@Configuration
public class WebAppConfig implements WebMvcConfigurer {
	@Value("${file.upload.path}")
	private String uploadPath; // 文件路径
	@Value("${file.upload.mapping}")
	private String showPath; // 映射路径

	@Autowired
	private LoginInterceptor loginInterceptor;

	/**
	 * interceptor配置
	 */
	@Override
	public void addInterceptors(InterceptorRegistry registry) {
		registry.addInterceptor(loginInterceptor)
				// 添加需要验证登录用户操作权限的请求
				.addPathPatterns("/**")
				// 排除不需要验证登录用户操作权限的请求
				.excludePathPatterns("/user/login/**", "/user/logout/**");

	}

	@Override
	public void addResourceHandlers(ResourceHandlerRegistry registry) {
		String a = showPath + "**";
		String b = "file:" + uploadPath;
		registry.addResourceHandler(a).addResourceLocations(b).setCacheControl(CacheControl.maxAge(2, TimeUnit.DAYS));
	}
}
